//
//  SpecialAudioListView.m
//  ImGuider
//
//  Created by 王鹏宇 on 2018/1/3.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "SpecialAudioListView.h"
#import "SpecailAudioHeaderView.h"
#import "SpecialAudioCell.h"
#import "IGAvPlayerManager.h"
@interface SpecialAudioListView() <UITableViewDataSource,UITableViewDelegate,SpecailAudioHeaderViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SpecailAudioHeaderView *headView;
@property (nonatomic) NSInteger sortType;// 0 默认，1最新

@end
@implementation SpecialAudioListView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}


- (void)setup {
    
    self.headView = [[SpecailAudioHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 53)];
    self.headView.sortType = self.sortType;
    self.headView.delegate = self;
    
    [self addSubview:self.headView];
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(self.mas_leading);
        make.top.equalTo(self.headView.mas_bottom);
        make.trailing.equalTo(self.mas_trailing);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SpecialAudioCell" bundle:nil] forCellReuseIdentifier:@"SpecialAudioCell"];
}

- (void)setSpecial:(LineModel *)special {
    _special = special;
    self.headView.sortType = special.sortType;
}
#pragma mark    -- UITableViewDataSource,UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.special.records.count;// self.SpecialAudioList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ScenicPoint *point = self.special.records[indexPath.row];
    
    SpecialAudioCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SpecialAudioCell"];
    
    cell.model = self.special;
    cell.point = point;
    cell.superVC = self.superVC;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_delegate && [_delegate respondsToSelector:@selector(selectIndex:)]) {
        [_delegate selectIndex:indexPath.row];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}
#pragma mark - SpecailAudioHeaderViewDelegate
- (void)SpecailAudioHeaderTapAction:(AudioHeaderNum)type {
    
    switch (type) {
        case default_Sequence: {
            
            self.special.sortType = 0;
            
            [IGAvPlayerManager shareManeger].lineModel = self.special;
            NSArray *sortArray = [self.special.records sortedArrayUsingComparator:^NSComparisonResult(ScenicPoint *  _Nonnull obj1, ScenicPoint *  _Nonnull obj2) {
                
                return obj1.number.integerValue > obj2.number.integerValue;
            }];
            
            self.special.records = sortArray;
            
        }
            break;
        case last_Sequence: {
            
           self.special.sortType = 1;
           [IGAvPlayerManager shareManeger].lineModel = self.special;
            NSArray *sortArray = [self.special.records sortedArrayUsingComparator:^NSComparisonResult(ScenicPoint *  _Nonnull obj1, ScenicPoint *  _Nonnull obj2) {
                
                return obj1.createtime > obj2.createtime;
            }];
            
            self.special.records = sortArray;
            
        }
            break;
        case download_All: {

            if (![IGUtil hasLoginOrPresentLogin]) {
                
                [self.superVC.sl_popupController dismiss];
                return;
            }
            
            [[DownloadManager sharedManager] downloadStatusForModel:self.special block:^(MCDownloadState state, NSInteger completeCount, NSString *unionid) {
                
                if (state == MCDownloadStateCompleted) {
                    return;
                }
                [[DownloadManager sharedManager] addDownloadWithLineModel:self.special];
            }];
        }
            break;
            
        default:
            break;
    }
    self.headView.sortType = self.special.sortType;
    
    [self.tableView reloadData];
}

@end
